﻿/**
 *	\file		lxpref_route.hxx
 *	\date		(2014-12-07 23:52:50)/(2014-12-07 23:52:50)
 *-----------------------------------------------------------------------------
 *	\brief
 *	\version	1.0.0.1
 *	\author		Nick Shallery	(nicknide@gmail.com)
 *	\copyright	YOU HAVE ALL LEFT WITH THIS CODE WHILE YOU KEEP COMMENT IN EVERY COPY.
 *-----------------------------------------------------------------------------
**/

#include	"./lxpref_route.h"

#if			!defined(__LOLIX__TOY__STD_LIST__NICK__K_IN_G__)
#	include	<lolix/toy/std_list.hpp>
#endif	//	!defined(__LOLIX__TOY__STD_LIST__NICK__K_IN_G__)


#if			!defined(__LOLIX__TOY__STD_VECTOR__NICK__K_IN_G__)
#	include	<lolix/toy/std_vector.hpp>
#endif	//	!defined(__LOLIX__TOY__STD_VECTOR__NICK__K_IN_G__)


#if			!defined(__LOLIX__TOY__STD_MAP__NICK__K_IN_G__)
#	include	<lolix/toy/std_map.hpp>
#endif 	//	!defined(__LOLIX__TOY__STD_MAP__NICK__K_IN_G__)


#if			!defined(__LOLIX__TOY__LOLI_PTR__NICK__K_IN_G__)
#	include	<lolix/toy/loli_ptr.hpp>
#endif	//	!defined(__LOLIX__TOY__LOLI_PTR__NICK__K_IN_G__)


namespace lolix{namespace loli{class file_async;class thread;class thread_factory;class critical_section; class critical_section_factory;class file_async_result;}}
namespace lolix{namespace toy{class stdc_fun;}}
namespace lxio{class file_async_pool_factory;class file_async_pool;}
namespace lxnet{class net_factory;class net_listen;class net_connect;}

namespace lxpref{namespace _inst{
	class lxpref_route_inst;
	class clt_usr;
}}


namespace lxpref{namespace message{
	enum PREF_MSG_TYPE LOLIX_ETYPE(lolix::lx_u32);
}}

namespace lxsamsara{class recurrence;}

class lxpref::_inst::lxpref_route_inst
	: public lolix::itf_Tbase<lxpref_route_inst, lxpref_route>
{
	LOLIX_DEFINE_ITF(lxpref::_inst::lxpref_route_inst, lxpref::lxpref_route, (0x8facef79, 0x8218413b, 0x92285adc, 0x9c50092c));
	lolix::dynamic_allocator*		_alc;
	lolix::toy::stdc_fun*			_stdc;
	lolix::loli::critical_section*	_cs_for_add_net_conn;
	lolix::loli::thread_factory*	_td_fac;
	lxio::file_async_pool_factory*	_async_fac;
	lxprofile* _prf;
	lxnet::net_factory* _net_fac;
	lolix::loli::thread* _td_view;
	lxnet::net_listen* _net_listen;
	lxsamsara::recurrence* _recur;

	lolix::toy::map<lolix::toy::loli_ptr<lolix::loli::file_async>, lolix::toy::loli_ptr<clt_usr>, lolix::toy::less, allocator_type> _net_conns;

	bool _need_exists_thread;

	lxpref_route_inst(lolix::dynamic_allocator* alc);
	~lxpref_route_inst(void);
public:
	static LOLIX_RETNV create_inst(this_type** out_ptr, lolix::dynamic_allocator* alc, lolix::ref_factory* ref_fac
									, lxprofile* prf
									, lolix::loli::critical_section_factory* cs_fac
									, lolix::loli::thread_factory* td_fac, lxnet::net_factory* net_fac
									, lolix::toy::stdc_fun* stdc
									, lxsamsara::recurrence* recur
									, lxio::file_async_pool_factory* async_pool_fac);
	LOLIX_INTERFACE_DEF(start	, LOLIX_RETNV	, (lolix::lx_wchar const* path, lolix::size_type path_len, lxio::file_async_pool* async_pool));
	LOLIX_INTERFACE_DEF(end		, LOLIX_RETNV	, (void));

private:
	LOLIX_RETNV __Start_conn(lolix::lx_wchar const* path, lolix::size_type path_len, lxio::file_async_pool* async_pool);
	LOLIX_RETNV LOLIX_CALL __Fetch_data_thread(lolix::loli::thread* td);
	static void LOLIX_CALL __On_conn(lxnet::net_connect* net_conn, void* usr_ptr);
	static void LOLIX_CALL __On_recvMsg_s(clt_usr* cusr, message::PREF_MSG_TYPE msgType, void const* buf, lolix::size_type buf_size, void* usr_ptr);
	void __On_recvMsg(clt_usr* cusr, message::PREF_MSG_TYPE msgType, void const* buf, lolix::size_type buf_size);
	void LOLIX_CALL __On_read(lolix::loli::file_async_result*);
};
